package ca.ubc.icapture.genapha.forms;

import ca.ubc.icapture.genapha.beans.Login;
import icapture.SQLMgr;
import icapture.beans.DB.Cohort;
import icapture.beans.DB.CohortPopulation;
import icapture.beans.DB.Ethnicity;
import icapture.beans.DB.Gene;
import icapture.beans.DB.Phenotypes;

import java.util.ArrayList;
import java.util.Iterator;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.validator.Resources;
import org.apache.struts.validator.ValidatorForm;

/**
 * Created by IntelliJ IDEA.
 * User: btripp
 * Date: Sep 13, 2006
 * Time: 9:39:23 AM
 * To change this template use File | Settings | File Templates.
 */

public class ExportForm2 extends ValidatorForm {

    // Fields: Start tab
    private ArrayList<Cohort> cohorts;
    private String[] selectedCohort;
    private ArrayList<CohortPopulation> subCohorts;
    private String[] selectedSubCohort;

    // Fields: Filter tab
    private boolean removeDuplicateSubjects;
    private boolean removeMzTwins;
    private boolean zeroIncomplete;
    private boolean zeroControls;
    private boolean hyperControls;
    private boolean caseControl;
    private ArrayList<Ethnicity> ethnicities;
    private String[] selectedEthnicity;

    // Fields: Output tab
    private ArrayList<Gene> genes;
    private String[] selectedGenes;
    private String[] selectedSnps;
    private ArrayList<Phenotypes> phenotypes;
    private String selectedPhenotype;
    private ArrayList<Phenotypes> covariates;
    private String[] selectedCovariates;
    private String format;
    private boolean filePerGene;

    private String submitType;
    
    public ExportForm2() {
        super();
        reset();

        cohorts = SQLMgr.getCohorts();
        Iterator<Cohort> cohortIter = cohorts.iterator();
        while (cohortIter.hasNext())
        {
            Cohort cohort = cohortIter.next();
            if (cohort.getName().equalsIgnoreCase("combined")
                || cohort.getName().contains("Hapmap")
                || cohort.getName().equalsIgnoreCase("SAGE-Controls"))
            {
                cohortIter.remove();
            }
        }

        subCohorts = SQLMgr.getCohortPopulations();
        for (CohortPopulation subCohort : subCohorts)
        {
            subCohort.setPopulationName(subCohort.getCohort().getName() + "-" + subCohort.getPopulationName());
        }

        genes = SQLMgr.getGenesNotInXChrom();
        phenotypes = SQLMgr.getPhenotypes(1);
        covariates = SQLMgr.getPhenotypes();
    }

    private void reset()
    {
        selectedCohort = null;
        selectedSubCohort = null;
        removeDuplicateSubjects = true;
        removeMzTwins = true;
        zeroIncomplete = false;
        zeroControls = false;
        hyperControls = false;
        caseControl = false;
        selectedEthnicity = null;
        selectedGenes = null;
        selectedSnps = null;
        selectedPhenotype = "";
        selectedCovariates = null;
        format = "";
        filePerGene = false;
        submitType = "";
    }

    @Override
    public void reset(ActionMapping mapping, HttpServletRequest request) {
        Login.LimitGenesToVisable(request.getSession(), genes);
        reset();
    }

    @Override
    public ActionErrors validate(ActionMapping mapping, HttpServletRequest request)
    {
        ActionErrors errors = super.validate(mapping, request);
        if (selectedEthnicity != null)
        {
            for (int i = 0; i < selectedEthnicity.length; i++)
            {
                if (selectedEthnicity[i].equalsIgnoreCase("-1"))
                {
                    errors.add("selectedEthnicity",
                            new ActionMessage("errors.invalid", Resources.getMessage(request, "export.ethnicity")));
                }
            }
        }

        return errors;
    }

    /**
     * @return the cohorts
     */
    public ArrayList<Cohort> getCohorts() {
        return cohorts;
    }

    /**
     * @param cohorts the cohorts to set
     */
    public void setCohorts(ArrayList<Cohort> cohorts) {
        this.cohorts = cohorts;
    }

    /**
     * @return the selectedCohort
     */
    public String[] getSelectedCohort() {
        return selectedCohort;
    }

    /**
     * @param selectedCohort the selectedCohort to set
     */
    public void setSelectedCohort(String[] selectedCohort) {
        this.selectedCohort = selectedCohort;
    }

    /**
     * @return the subCohorts
     */
    public ArrayList<CohortPopulation> getSubCohorts() {
        return subCohorts;
    }

    /**
     * @param subCohorts the subCohorts to set
     */
    public void setSubCohorts(ArrayList<CohortPopulation> subCohorts) {
        this.subCohorts = subCohorts;
    }

    /**
     * @return the selectedCohortSubPopulation
     */
    public String[] getSelectedSubCohort() {
        return selectedSubCohort;
    }

    /**
     * @param selectedSubCohort the selectedSubCohort to set
     */
    public void setSelectedSubCohort(String[] selectedSubCohort) {
        this.selectedSubCohort = selectedSubCohort;
    }

    /**
     * @return the removeDuplicateSubjects
     */
    public boolean isRemoveDuplicateSubjects() {
        return removeDuplicateSubjects;
    }

    /**
     * @param removeDuplicateSubjects the removeDuplicateSubjects to set
     */
    public void setRemoveDuplicateSubjects(boolean removeDuplicateSubjects) {
        this.removeDuplicateSubjects = removeDuplicateSubjects;
    }

    /**
     * @return the removeMzTwins
     */
    public boolean isRemoveMzTwins() {
        return removeMzTwins;
    }

    /**
     * @param removeMzTwins the removeMzTwins to set
     */
    public void setRemoveMzTwins(boolean removeMzTwins) {
        this.removeMzTwins = removeMzTwins;
    }

    /**
     * @return the zeroIncomplete
     */
    public boolean isZeroIncomplete() {
        return zeroIncomplete;
    }

    /**
     * @param zeroIncomplete the zeroIncomplete to set
     */
    public void setZeroIncomplete(boolean zeroIncomplete) {
        this.zeroIncomplete = zeroIncomplete;
    }

    /**
     * @return the zeroControls
     */
    public boolean isZeroControls() {
        return zeroControls;
    }

    /**
     * @param zeroControls the zeroControls to set
     */
    public void setZeroControls(boolean zeroControls) {
        this.zeroControls = zeroControls;
    }

    /**
     * @return the hyperControls
     */
    public boolean isHyperControls() {
        return hyperControls;
    }

    /**
     * @param hyperControls the hyperControls to set
     */
    public void setHyperControls(boolean hyperControls) {
        this.hyperControls = hyperControls;
    }

    /**
     * @return the caseControl
     */
    public boolean isCaseControl() {
        return caseControl;
    }

    /**
     * @param caseControl the caseControl to set
     */
    public void setCaseControl(boolean caseControl) {
        this.caseControl = caseControl;
    }

    /**
     * @return the ethnicities
     */
    public ArrayList<Ethnicity> getEthnicities() {
        return ethnicities;
    }

    /**
     * @param ethnicities the ethnicities to set
     */
    public void setEthnicities(ArrayList<Ethnicity> ethnicities) {
        this.ethnicities = ethnicities;
    }

    /**
     * @return the selectedEthnicity
     */
    public String[] getSelectedEthnicity() {
        return selectedEthnicity;
    }

    /**
     * @param selectedEthnicity the selectedEthnicity to set
     */
    public void setSelectedEthnicity(String[] selectedEthnicity) {
        this.selectedEthnicity = selectedEthnicity;
    }

    /**
     * @return the genes
     */
    public ArrayList<Gene> getGenes() {
        return genes;
    }

    /**
     * @param genes the genes to set
     */
    public void setGenes(ArrayList<Gene> genes) {
        this.genes = genes;
    }

    /**
     * @return the selectedGenes
     */
    public String[] getSelectedGenes() {
        return selectedGenes;
    }

    /**
     * @param selectedGenes the selectedGenes to set
     */
    public void setSelectedGenes(String[] selectedGenes) {
        this.selectedGenes = selectedGenes;
    }

    /**
     * @return the selectedSnps
     */
    public String[] getSelectedSnps() {
        return selectedSnps;
    }

    /**
     * @param selectedSnps the selectedSnps to set
     */
    public void setSelectedSnps(String[] selectedSnps) {
        this.selectedSnps = selectedSnps;
    }

    /**
     * @return the phenotypes
     */
    public ArrayList<Phenotypes> getPhenotypes() {
        return phenotypes;
    }

    /**
     * @param phenotypes the phenotypes to set
     */
    public void setPhenotypes(ArrayList<Phenotypes> phenotypes) {
        this.phenotypes = phenotypes;
    }

    /**
     * @return the selectedPhenotype
     */
    public String getSelectedPhenotype() {
        return selectedPhenotype;
    }

    /**
     * @param selectedPhenotype the selectedPhenotype to set
     */
    public void setSelectedPhenotype(String selectedPhenotype) {
        this.selectedPhenotype = selectedPhenotype;
    }

    /**
     * @return the covariates
     */
    public ArrayList<Phenotypes> getCovariates() {
        return covariates;
    }

    /**
     * @param covariates the covariates to set
     */
    public void setCovariates(ArrayList<Phenotypes> covariates) {
        this.covariates = covariates;
    }

    /**
     * @return the selectedCovariates
     */
    public String[] getSelectedCovariates() {
        return selectedCovariates;
    }

    /**
     * @param selectedCovariates the selectedCovariates to set
     */
    public void setSelectedCovariates(String[] selectedCovariates) {
        this.selectedCovariates = selectedCovariates;
    }

    /**
     * @return the format
     */
    public String getFormat() {
        return format;
    }

    /**
     * @param format the format to set
     */
    public void setFormat(String format) {
        this.format = format;
    }

    /**
     * @return the isFilePerGene
     */
    public boolean isFilePerGene() {
        return filePerGene;
    }

    /**
     * @param filePerGene the filePerGene to set
     */
    public void setFilePerGene(boolean filePerGene) {
        this.filePerGene = filePerGene;
    }

    /**
     * @return the submitType
     */
    public String getSubmitType() {
        return submitType;
    }

    /**
     * @param submitType the submitType to set
     */
    public void setSubmitType(String submitType) {
        this.submitType = submitType;
    }
}